<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/29
 * Time: 22:19
 */

namespace Xcx\Model;

use Xcx\Common\ErrorCode;

class UserCouponModel extends BaseModel
{

    public $errMsg;
    public $code;

    public function getErrMsg()
    {
        return $this->errMsg;
    }

    public function getErrCode()
    {
        return $this->code;
    }

    /**
     * 根据订单金额获取对应优惠力度最大的优惠券
     * @param $totalFee 订单总金额
     * @return bool
     */
    public function getDefaultCoupon($totalFee)
    {
        $maps['a.uid'] = $this->uid;
        $maps['a.start_date'] = array('ELT',strtotime(date('Ymd',time())));
        $maps['a.end_date'] = array('EGT',strtotime(date('Ymd',time())));
        $maps['a.state'] = 1;
        $maps['b.use_con'] = array('ELT',$totalFee);
        $data = $this->alias('a')
            ->field('a.id,a.cid,b.intro,b.price')
            ->join('left join t_goods_coupon b on a.cid = b.id')
            ->where($maps)
            ->order(array('b.price'=>'desc','a.end_date'=>'desc','a.add_time'=>'asc'))
            ->find();

        if (is_array($data) && count($data) > 0) return $data;
        return false;
    }

    /**
     * 获取用户的优惠券列表
     * @return bool
     */
    public function getUserGoodsCouponList() {
        $maps['a.uid'] = $this->uid;
        $maps['a.start_date'] = array('ELT',strtotime(date('Ymd',time())));
        $maps['a.end_date'] = array('EGT',strtotime(date('Ymd',time())));
        $maps['a.state'] = 1;
        $data = $this->alias('a')
            ->field('a.*,b.price,b.use_con,b.intro')
            ->join('left join t_goods_coupon b on a.cid = b.id')
            ->where($maps)
            ->order(array('b.price'=>'desc','a.end_date'=>'desc','a.add_time'=>'asc'))
            ->select();

        if (is_array($data) && count($data) > 0) return $data;
        return false;
    }

    /**
     * 商品优惠券领取
     * @param $id
     * @return bool
     */
    public function receiveGoodsCoupons($id)
    {
        $couponInfo = $this->getGoodsCouponInfo($id);
        $userInfo = $this->getUserInfo();

        if ($couponInfo['remain'] <= 0) {
            $this->code = ErrorCode::$RemainCountFail;
            $this->errMsg = '优惠券余量不足';
            return false;
        }
        if ($userInfo['all_praise'] - $userInfo['use_praise'] < $couponInfo['change_count']) {
            $this->code = ErrorCode::$UserScoreCountFail;
            $this->errMsg = '您的积分不足';
            return false;
        }

        M()->startTrans();
        $res1 = $this->addUserCoupon($id, $couponInfo['time_len']);
        $res2 = $this->setIncUsePraiseCount($couponInfo['change_count']);
        $res3 = $this->setDecGoodsCouponRemain($id);

        if ($res1 && $res2 && $res3) {
            M()->commit();
            return true;
        } else {
            M()->rollback();
            $this->errMsg = '兑换失败';
            return false;
        }
    }

    /**
     * 设置优惠券剩余数量
     * @param $id
     * @return bool|int|true
     */
    private function setDecGoodsCouponRemain($id)
    {
        $GoodsCoupon = new GoodsCouponModel();
        return $GoodsCoupon->setDecGoodsCouponRemain($id);
    }

    /**
     * 添加用户优惠券
     * @param $cid  优惠券ID
     * @param $timeLen 有效时长
     * @return mixed
     */
    private function addUserCoupon($cid,$timeLen)
    {
        $data['uid'] = $this->uid;
        $data['cid'] = $cid;
        $data['start_date'] = strtotime(date('Ymd',time()));
        $data['end_date'] = strtotime(date('Ymd',time()+$timeLen * 24 *3600));;
        $data['add_time'] = time();

        return $this->data($data)->add();
    }

    /**
     * 增加用户积分消费总量
     * @param $count
     * @return bool|int|true
     */
    private function setIncUsePraiseCount($count)
    {
        $User = new UsersModel();
        return $User->setIncUsePraiseCount($count);
    }

    /**
     * 获取用户信息
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     */
    private function getUserInfo()
    {
        $User = new UsersModel();
        return $User->getUserInfo();
    }

    /**
     * 获取商品优惠券信息
     * @param $id
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     */
    private function getGoodsCouponInfo($id)
    {
        $GoodsCoupon = new GoodsCouponModel();
        return $GoodsCoupon->getGoodsCouponInfo($id);
    }

    /**
     * 获取用户优惠券信息
     * @param $id
     * @return mixed
     */
    public function getUserCouponInfo($id)
    {
        return $this->alias('a')
            ->field('a.*,b.price')
            ->join('left join t_goods_coupon b on a.cid = b.id')
            ->where(array('a.id'=>$id))
            ->find();
    }

    public function updateCouponState($id,$state)
    {
        $maps['id'] = $id;
        $maps['uid'] = $this->uid;

        return $this->where($maps)->data(array('state'=>$state))->save();
    }
}